package com.qiezikaka.kaka.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.qiezikaka.kaka.net.DataManager;
import com.qiezikaka.kaka.utils.SharedPreferencesUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;


/**
 * @Description activity的基类
 */
public abstract class BaseActivity<T extends IPresenter> extends AppCompatActivity {

    public Context mContext;
    private T mPresenter;
    private Dialog mDialog;
    public Bundle mSavedInstanceState;
    private int mSb_h;
    private boolean isRegistered = false;
    public QMUITipDialog mTipDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.translucent(this);
        hideBottomUIMenu();
//        QMUIStatusBarHelper.setStatusBarLightMode(this);
        mSavedInstanceState = savedInstanceState;
        mContext = this;
        mTipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("正在加载")
                .create();
        appStatusManager();
    }

    private void appStatusManager() {
        switch (AppStatusManager.getInstance().getAppStatus())
        {
            /*应用正常运行*/
            case AppStatusConstant.APP_NORMAL:
                /*初始化*/
                //初始化信息管理类
                DataManager.getInstance().initService();
                setContentView(getLayout());
                mPresenter = getPresenter();
                initEventAndData();
                break;
            /*应用进程被系统回收*/
            case AppStatusConstant.APP_FORCE_KILLED:
//                showToast("系统回收");
                restartApp(this);
                break;
        }
    }

    /* 隐藏虚拟按键，并且全屏 */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            v.setSystemUiVisibility(uiOptions);
        } else {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }



    public static void restartApp(Activity activity) {
//        Intent intent=new Intent(activity, SplashActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        activity.startActivity(intent);
//        activity.finish();
    }


    public void showDialogLoading() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void disMissLoading() {
        if (mDialog != null && mDialog.isShowing())
            mDialog.dismiss();
    }

    /**
     * 判断是否缺少权限
     */
    public boolean lacksPermission(Context mContexts, String permission) {
        return ContextCompat.checkSelfPermission(mContexts, permission) ==
                PackageManager.PERMISSION_GRANTED;
    }
    /**
     * 判断权限集合
     * permissions 权限数组
     * return true-表示没有改权限  false-表示权限已开启
     */
    public boolean lacksPermissions(String [] mPermissions) {
        for (String permission : mPermissions) {
            if (lacksPermission(this, permission)) {
                Log.e("TAG","-------没有开启权限");
                return true;
            }
        }
        Log.e("TAG","-------权限已开启");
        return false;
    }


    //获取布局
    public abstract View getLayout();

    //获取presenter
    protected abstract T getPresenter();

    //初始化事件和数据
    protected abstract void initEventAndData();

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    /**
     * 弹吐司
     */
    public void showToast(String connect) {
        Toast.makeText(mContext, connect, Toast.LENGTH_SHORT).show();
    }

    /**
     * 关闭软键盘
     */
    public void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

}
